<?php
/**
 * @brief 
 *
 * @license		http://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License
 * @copyright	2008 David Singer
 * @author		David Singer <david@ramaboo.com>
 * @version		1.0.0
 */

require_once('../../includes/common.php');

try {
	// check required params
	if (!isset($_POST['id'])) {
		throw new Exception("Id is required.");
	}
	$id = intval($_POST['id']);
	
	if (!isset($_POST['field'])) {
		throw new Exception("Field is required.");
	}
	$field = htmlentities(strtolower(trim($_POST['field'])));
	
	if (!isset($_POST['value'])) {
		throw new Exception("Value is required.");
	}
	$value = htmlentities(strtolower(trim($_POST['value'])));
	
	$json = array();
	$record = new Record;
	
	$record->open($id);
		
	switch ($field) {
		case 'host':
			$record->setHost($value);
			break;
			
		case 'data':
			$record->setData($value);
			break;
			
		case 'ttl':
			$record->setTTL($value);
			break;
			
		default:
			throw new Exception("Field is not valid.");
	}
	
	$record->save();
	
	// made it
	$json['success'] = true;
	$json['msg'] = "Record $id saved.";
} catch (Exception $e) {
	$json['success'] = false;
	$json['msg'] = $e->getMessage();
}

$output = json_encode($json);
header_no_cache(strlen($output), 'application/json');
echo $output;
?>